/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.gbvbahia.projeto.service;

import br.com.gbvbahia.projeto.beans.facade.interfaces.UsuarioFacade;
import br.com.gbvbahia.utils.MensagemUtils;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 *  http://pobrecomputeiro.wordpress.com/2012/04/22/a-simplicidade-do-spring-security/
 * Busca usuário de acordo com login.
 * @since 30/04/2012
 * @author Guilherme
 */
@Component("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    /**
     * Fachada de acesso a usuário.
     */
    @Autowired
    private UsuarioFacade userFacade;

    @Override
    public UserDetails loadUserByUsername(final String string)
            throws UsernameNotFoundException, DataAccessException {
        UserDetails user = (UserDetails) userFacade.find(string);
        if (user == null) {
            String aviso =
                    MensagemUtils.getResourceBundle("loginInvalido",
                    FacesContext.getCurrentInstance());
            throw new UsernameNotFoundException(aviso);
        }
        return user;
    }
}
